package com.ruantu.clothing.controller;

import com.ruantu.clothing.entity.*;
import com.ruantu.clothing.entity.combobox.RukuTuiCombobox;
import com.ruantu.clothing.service.RukuService;
import com.ruantu.clothing.util.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/6/22 0022.
 */
@Controller
@RequestMapping("/ruku")
public class RukuController {

    private static final Logger logger = LoggerFactory.getLogger(RukuController.class);
    @Autowired
    private RukuService rukuService;

    @RequestMapping("/list")
    @ResponseBody
    public JsonResponseMessage findList(Ruku ruku, PageBean pageBean) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(rukuService.findRukuList(ruku, pageBean));
        jsonResponseMessage.setTotal(pageBean.getTotal());
        return jsonResponseMessage;
    }

    @RequestMapping("/childrenlist/id")
    @ResponseBody
    public JsonResponseMessage findRukuId(Rukuson rukuson, Integer fenlei) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(rukuService.findRukuListSonById(rukuson, fenlei));
        return jsonResponseMessage;
    }

    /**
     * 提交入库单
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("/tijiao")
    @ResponseBody
    public JsonResponseMessage tijiao(Ruku ruku, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        try {
            ruku.setShenheren(user.getId());
            rukuService.commitRukudan(ruku);
            jsonResponseMessage.setCode(200);

        } catch (MyException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (NullPointerException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请重新登录");
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请刷新重新尝试");
            logger.error(e.getMessage(), e);
        }
        return jsonResponseMessage;
    }


    @RequestMapping("/tailorruku/id")
    @ResponseBody
    public JsonResponseMessage findTailorRuku(String id) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            List<MyEntity> list = rukuService.findTailorRuku(id);
            jsonResponseMessage.setData(list);
            List<String> colors = new ArrayList<>();
            List<String> sizes = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if (!colors.contains(list.get(i).getColor())) {
                    colors.add(list.get(i).getColor());
                }
                if (!sizes.contains(list.get(i).getSize())) {
                    sizes.add(list.get(i).getSize());
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("color", colors);
            map.put("size", sizes);
            jsonResponseMessage.setRows(map);
            jsonResponseMessage.setCode(200);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("内部服务器错误，请检查数据重新尝试");
        }
        return jsonResponseMessage;
    }

    /**
     * 保存入库单
     *
     * @param
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JsonResponseMessage save(String ruku, Ruku rukus, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        try {
            rukus.setJgId(user.getJgid());
            rukus.setZhidanren(user.getId());
            rukus.setUpdateBy(user.getId());
            rukuService.saveRuku(ruku, rukus);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        } catch (NullPointerException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请刷新重试");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("内部服务器错误，请检查数据重新尝试");
        }
        return jsonResponseMessage;
    }

    /**
     * 保存退货入库单
     *
     * @param
     * @return
     */
    @RequestMapping("/savetui")
    @ResponseBody
    public JsonResponseMessage saveTui(String ruku, Ruku rukus, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        try {
            rukus.setJgId(user.getJgid());
            rukus.setZhidanren(user.getId());
            rukus.setUpdateBy(user.getId());
            rukuService.saveTui(ruku, rukus);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        } catch (NullPointerException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请刷新重试");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("内部服务器错误，请检查数据重新尝试");
        }
        return jsonResponseMessage;
    }


    /**
     * 入库入库单
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("/addruku")
    @ResponseBody
    public JsonResponseMessage addRuku(Ruku ruku, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        try {
            ruku.setJgId(user.getJgid());
            ruku.setRukuren(user.getId());
            rukuService.addRuku(ruku);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (NullPointerException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请重新登录");
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请刷新重新尝试");
            logger.error(e.getMessage(), e);
        }
        return jsonResponseMessage;
    }

    /**
     * 减少入库单
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("/tuihuo")
    @ResponseBody
    public JsonResponseMessage Tuihuo(Ruku ruku, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        try {
            ruku.setJgId(user.getJgid());
            ruku.setRukuren(user.getId());
            rukuService.tuiHuo(ruku);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (NullPointerException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请重新登录");
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请刷新重新尝试");
            logger.error(e.getMessage(), e);
        }
        return jsonResponseMessage;
    }

    /**
     * 删除入库单
     *
     * @return
     */
    @RequestMapping("/del/id")
    @ResponseBody
    public JsonResponseMessage del(Ruku ruku) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            rukuService.delRuku(ruku);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务其内部错误，请检查数据重新尝试");
            logger.error(e.getMessage(), e);
        }
        return jsonResponseMessage;
    }

    /**
     * 删除入库单
     *
     * @return
     */
    @RequestMapping("/bohui/id")
    @ResponseBody
    public JsonResponseMessage bohui(Ruku ruku, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        try {
            ruku.setBohuiren(user.getId());
            rukuService.bohui(ruku);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请检查数据重新尝试");
            logger.error(e.getMessage(), e);
        }
        return jsonResponseMessage;
    }


    /**
     * 修改入库单反显
     *
     * @return
     */
    @RequestMapping("/findxiugai/id")
    @ResponseBody
    public JsonResponseMessage findXiugaiById(Ruku ruku, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            User user = (User) session.getAttribute("info");
            ruku.setJgId(user.getJgid());
            jsonResponseMessage.setCode(200);
            jsonResponseMessage.setData(rukuService.findRukuByRukuId(ruku));
        } catch (NullPointerException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请重新登录尝试");
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误");
            logger.error(e.getMessage(), e);
        }
        return jsonResponseMessage;
    }

    @RequestMapping("/rukudan/product")
    @ResponseBody
    public List<Ruku> findRukudan(String q, Ruku ruku, HttpSession session) {
        User user = (User) session.getAttribute("info");
        ruku.setJgId(user.getJgid());
        return rukuService.findRukuDan(ruku, q);
    }

    /**
     * 根据入库单id查询商品详细信息
     *
     * @param ruku
     * @return
     */
    @RequestMapping("/findproduct")
    @ResponseBody
    public List<RukuTuiCombobox> findProductByRukuId(Ruku ruku) {
        return rukuService.findProductByRukuId(ruku);
    }

    /**
     * 根据入库单id和款号id查询商品详细信息
     *
     * @param ruku
     * @return
     */
    @RequestMapping("/details")
    @ResponseBody
    public JsonResponseMessage findDetails(Ruku ruku) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        List<RukuTuiCombobox> list = rukuService.findDetails(ruku);
        jsonResponseMessage.setData(list);
        List<String> colors = new ArrayList<>();
        List<String> sizes = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (!colors.contains(list.get(i).getColor())) {
                colors.add(list.get(i).getColor());
            }
            if (!sizes.contains(list.get(i).getSize())) {
                sizes.add(list.get(i).getSize());
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("color", colors);
        map.put("size", sizes);
        jsonResponseMessage.setRows(map);
        jsonResponseMessage.setCode(200);
        return jsonResponseMessage;
    }


    /**
     * 修改入库单反显
     *
     * @return
     */
    @RequestMapping("/findxiugaitui/id")
    @ResponseBody
    public JsonResponseMessage findXiugaiTuiById(Ruku ruku, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            User user = (User) session.getAttribute("info");
            ruku.setJgId(user.getJgid());
            jsonResponseMessage.setCode(200);
            jsonResponseMessage.setData(rukuService.findRukuTuiByRukuId(ruku));
        } catch (NullPointerException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请重新登录尝试");
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误");
            logger.error(e.getMessage(), e);
        }
        return jsonResponseMessage;
    }


}
